from decimal import Decimal

from django.shortcuts import render

# Create your views here.

from django_redis import get_redis_connection
from rest_framework import status
from rest_framework.generics import GenericAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from goods.models import SKU
from orders.serializers import OrderSKUSerializer, OrderSerializer


# GET /orders/settlement/
class OrdersSettlementView(APIView):
    """获取订单商品的数据"""
    permission_classes = [IsAuthenticated]

    def get(self, request):
        # 1. 从redis购物车记录中获取登录用户所要结算的商品的id和对应的数量count
        user = request.user

        redis_conn = get_redis_connection('cart')

        # 从redis set中获取用户购物车勾选的商品的id
        cart_selected_key = 'cart_select_%s' % user.id

        sku_ids = redis_conn.smembers(cart_selected_key)

        # 从redis hash 中获取用户购物车商品的id和对应的数量count
        cart_key = 'cart_%s' % user.id

        redis_cart = redis_conn.hgetall(cart_key)

        # 2. 根据商品的id多去对应商品的信息并组织运费
        skus = SKU.objects.filter(id__in=sku_ids)

        cart = {}

        for sku_id, count in redis_cart.items():
            cart[int(sku_id)] = int(count)

        for sku in skus:
            # 获取该商品所要结算的数量
            sku.count = cart[sku.id]

        # 组织运费
        freight = Decimal(10)
        # 3. 将商品的数据序列化并返回
        serializer = OrderSKUSerializer(skus, many=True)

        response_data = {
            'freight': freight,
            'skus': serializer.data
        }
        return Response(response_data)


# POST /orders/
class OrdersView(GenericAPIView):
    permission_classes = [IsAuthenticated]
    serializer_class = OrderSerializer

    def post(self, request):
        """
        订单信息的保存
        1. 接收参数并进行校验(参数完整性, 地址是否存在,支付方式是否合法)
        2. 创建并保存订单的数据
        3. 返回应答,订单创建成功

        """
        # 1.接收参数并进行校验(参数完整性,地址是否存在,支付方式是否合法)
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        # 2.创建并保存订单的数据
        serializer.save()

        # 3.返回应答, 订单创建成功
        return Response(serializer.data, status=status.HTTP_201_CREATED)

